/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io.osw.common.utils;

import java.util.Collection;

/**
 *
 * @author Administrator
 */
public class StringsUtil {
    
    public static String jsAlert(String message)
    {
        return new StringBuilder("<script type='text/javascript'>alert('")
                .append(message)
                .append("');</script>")
                .toString();
    }
    
    public static String omAlert(String type, String title, String message)
    {
        return new StringBuilder("<script type='text/javascript'>top.omAlert('")
                .append(type)
                .append("','")
                .append(title)
                .append("','")
                .append(message)
                .append("');</script>")
                .toString();
    }
    
    public static String omAlert(String type, String title, String message, String url)
    {
        return new StringBuilder("<script type='text/javascript'>top.omAlert('")
                .append(type)
                .append("','")
                .append(title)
                .append("','")
                .append(message)
                .append("','")
                .append(url)
                .append("');</script>")
                .toString();
    }
    
    public static Integer toInteger(String str)
    {
        str = filterNoNumberChars(str);
        if (str == null || str.isEmpty())
        {
            return null;
        }
        if (isNumberic(str))
        {
            return Integer.parseInt(str);
        }
        return null;
    }
    
    public static Long  toLong(String str)
    {
        str = filterNoNumberChars(str);
        if (str == null || str.isEmpty())
        {
            return null;
        }
        return Long.parseLong(str);
    }
    
    public static Double toDouble(String str)
    {
        str = filterNoNumberChars(str);
        if (str == null || str.isEmpty())
        {
            return null;
        }
        return Double.parseDouble(str);
    }
    
    public static boolean isNumberic(String str)
    {
        for (int len = str.length(); -- len >= 0; )
        {
            if (!Character.isDigit(str.charAt(len)))
            {
                return false;
            }
        }
        return true;
    }
    
    private static String  filterNoNumberChars(String str)
    {
        char[]  tmpCa   = str.toCharArray();
        StringBuilder sbd = new StringBuilder(tmpCa.length);
        for (int i = 0; i < tmpCa.length; ++ i)
        {
            if ((tmpCa[i] >= '0' && tmpCa[i] <= '9') || tmpCa[i] == '.')
            {
                sbd.append(tmpCa[i]);
            }
        }
        return sbd.toString();
    }
    
    public static boolean isChinese(char c)
    {
        Character.UnicodeBlock  ub  = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C)
        {
            return true;
        }
        return false;
    }
    
    public static boolean isChinese(String str)
    {
        for (int len = str.length(); -- len >= 0;)
        {
            char    c   = str.charAt(len);
            if (!isChinese(c))
            {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isDoubleNumber(String str)
    {
        for (int len = str.length(); -- len >= 0; )
        {
            char c  = str.charAt(len);
            if (!Character.isDigit(c) && c != '.')
            {
                return false;
            }
        }
        return true;
    }
    
    public static String  implode(String glue, Collection picese)
    {
        StringBuilder sbd   = new StringBuilder();
        boolean first       = true;        
        for (Object o : picese)
        {
            if (!first)
            {
                sbd.append(glue);
            } else {
                first   = false;
            }
            sbd.append(o.toString());
        }
        return sbd.toString();
    }
    
    public static String implode(String glue, String[] picese)
    {
        StringBuilder sbd   = new StringBuilder();
        boolean first       = true;        
        for (Object o : picese)
        {
            if (!first)
            {
                sbd.append(glue);
            } else {
                first   = false;
            }
            sbd.append(o);
        }
        return sbd.toString();
    }
    
    public  static String implode(char glue, int[] picese) {
        StringBuilder sbd = new StringBuilder();
        boolean first   = true;
        for (int i : picese) {
            if (!first) {
                sbd.append(glue);
            } else {
                first   = false;
            }
            sbd.append(i);
        }
        return sbd.toString();
    }
    
    public  static String implode(char glue, long[] picese) {
        StringBuilder sbd = new StringBuilder();
        boolean first   = true;
        for (long l : picese) {
            if (!first) {
                sbd.append(glue);
            } else {
                first   = false;
            }
            sbd.append(l);
        }
        return sbd.toString();
    }
}
